<?php
namespace App\Services\Sms\Sms;
use App\Models\Sms\Sms;
use App\Services\Sms\ValidateSms;
use App\Services\Manage\ManageIndex;
use Illuminate\Support\Facades\DB;
/**
 * @Author: 蒋哲
 * @Date:   2017-05-27 16:25:06
 * @Last Modified by:   ws
 * @Last Modified time: 2018-06-12 
 */
class SmsLogic 
{
    //内部db查询该条记录信息
    /*public function dbOneInfo($data)
    {
        $_where['id'] = $data['id'];
        $_where['status'] = isset($data['status']) ? $data['status'] : ['neq', -1];
        return $this->db()
                    ->where($_where)
                    ->find();
    }*/

    /*检验识别码的唯一性*/
   /* public function checkAction($data): array 
    {
        $info = $this->db()->where(['action'=>$data['action']])->find();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }*/

    /*发送单条信息*/   
   /* public function sendOne($data): array 
    {
        $sms = new $data['smsName']();
        $res = $sms->sendOneSms($data);

        $logData['mobile']     = $data['send_info']['mobile'];
        $logData['text']       = $data['send_info']['text'];
        $logData['status']     = $res['code'];
        $logData['status_msg'] = $res['msg'];
        logic('app\serversms\logic\SmsLog')->addLog($logData); //添加短信发送记录

        if($res['code'] != 0){  
            return auto_failure($res, $res['detail']);
        }
        return auto_success($res, '成功发送短信');
    }*/

    /*发送多条短信*/
    /*public function batchSend($data): array 
    {
        $sms = new $data['smsName']();
        $res = $sms->sendBatchSms($data);

        $logData = [];

        if(isset($res['data'])){
            foreach ($res['data'] as $k => $v) {
                $logData[$k]['mobile']     = $v['mobile'];
                $logData[$k]['text']       = $data['send_info']['text'];
                $logData[$k]['status']     = $v['code'];
                $logData[$k]['status_msg'] = $v['msg'];
            }
            logic('app\serversms\logic\SmsLog')->batchAddLog($logData); //批量添加短信发送记录 
        }

        if(isset($res['total_count']) && $res['total_count'] > 0){
            return auto_success($res, '成功发送' . $res['total_count'] . '条短信');
        }else{
            return auto_failure($res, $res['detail']);
        }
        
    }*/

    /*添加单个短信配置*/
    public function addSms($data): array 
    {
        $data['sms_info'] = json_encode($data['sms_info']); 
        $data['create_time'] = time();
        $data['update_time'] = time();
        $res = Sms::insertGetId($data);
        return auto_return($res);
    }

    /*获取某个应用下的所有短信配置*/
    public function smsList($data): array 
    {
        $res = cache('sms_smsList');   
        $res = 0;  
        $app_id =  $data['app_id'];
        //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        if(!$res){
            if(!empty($app_id) && $manageAppId != $app_id){
               $res = Sms::where('status','=',1) 
                     ->where(function($query) use ($app_id){
                     $query->orWhere('app_id', '=', $app_id)
                     ->orWhereRaw ("FIND_IN_SET($app_id,other_app_read)")
                     ->orWhere('is_public', '=', 1); }) 
                     ->orderBy('id', 'desc')
                     ->paginate($data['listRows'], null, null, $data['page'])
                     ->toArray();   
            }else{
                $res = Sms::where('status','=',1) 
                     ->orderBy('id', 'desc')
                     ->paginate($data['listRows'], null, null, $data['page'])
                     ->toArray();
            }
            cache('sms_smsList', $res, 86400);
        }
        return auto_return_page($res);
    }

    /*获取某个短信配置详情*/
    public function smsInfo($data): array 
    {
        //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        $app_id = $data['app_id'];
        $re = Sms::where('status', '=', 1);          
        if(!empty($app_id) && $manageAppId != $app_id){
            $re->where(function($query) use ($app_id){
                     $query->orWhere('app_id', '=', $app_id)
                     ->orWhereRaw ("FIND_IN_SET($app_id,other_app_read)")
                     ->orWhere('is_public', '=', 1); });
        }
        if($data['id']){
            $re->where('id','=',$data['id']);
        }
        if($data['action']){
            $re->where('action','=',$data['action']);
        }  
        $res = $re->first()->toArray();
        return auto_return($res, '成功获取短信配置', '获取短信配置失败');
    }

    /*修改某个短信配置*/
    public function editSms($data): array 
    {
        //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        $app_id = $data['app_id'];
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['sms_info'] = json_encode($data['sms_info']); 
        if($manageAppId != $app_id && !empty($app_id)){
            unset($data['app_id']);
            $res = Sms::where([['id','=',$data['id']],['status','=',1],['app_id','=',$app_id]])->update($data);
        }else{
           unset($data['app_id']);
           $res = Sms::where([['id','=',$data['id']],['status','=',1]])->update($data); 
        }
        cache('sms_smsList', NULL);   //清空缓存
        return auto_return($res);
    }

    /*删除某个短信配置*/
    public function delOneSms($data): array 
    {
        //获得总后台的APP_id
        $manageAppId = ManageIndex::Api()->getAppId();
        $app_id = $data['app_id'];
        if($manageAppId != $app_id && !empty($app_id)){
        $res = Sms::where([['id','=',$data['id']],['status','=',1],['app_id','=',$app_id]])->update(['status' => -1]);
        }else{
        $res = Sms::where([['id','=',$data['id']],['status','=',1]])->update(['status' => -1]);     
        }
        return auto_return($res);
    }

}